import * as vscode from 'vscode';
import { SmlFormatter } from './smlFormatter';

export function activate(context: vscode.ExtensionContext) {
    console.log('SML Language Support activated');

    // 注册执行当前行的命令
    const executeLineCommand = vscode.commands.registerCommand('sml.executeLine', async () => {
        const editor = vscode.window.activeTextEditor;
        if (!editor) {
            vscode.window.showInformationMessage('请先打开一个编辑器窗口');
            return;
        }

        // 检查当前文件是否为 SML 类型
        if (editor.document.languageId !== 'sml') {
            vscode.window.showWarningMessage('当前文件不是 SML 文件');
            return;
        }

        const terminal = vscode.window.activeTerminal;
        if (!terminal) {
            vscode.window.showWarningMessage('请先打开一个 VS Code 集成终端，并确保已启动 SML REPL');
            return;
        }

        // 获取选中的文本或当前行
        let textToExecute = '';
        const selection = editor.selection;

        if (!selection.isEmpty) {
            // 有选中的文本
            textToExecute = editor.document.getText(selection);
            // 移动光标到选中区域的下一行
            const endLine = editor.document.lineAt(selection.end.line);
            const newPosition = new vscode.Position(endLine.lineNumber + 1, 0);
            editor.selection = new vscode.Selection(newPosition, newPosition);
        } else {
            // 没有选中的文本，获取当前行
            const currentLine = editor.document.lineAt(selection.active.line);
            textToExecute = currentLine.text.trim();

            // 如果是空行或注释行，则只移动光标
            if (textToExecute === '' || textToExecute.startsWith('(*') || textToExecute.startsWith('//')) {
                const newPosition = new vscode.Position(selection.active.line + 1, 0);
                editor.selection = new vscode.Selection(newPosition, newPosition);
                return;
            }

            // 移动光标到下一行
            const newPosition = new vscode.Position(selection.active.line + 1, 0);
            editor.selection = new vscode.Selection(newPosition, newPosition);
        }

        // 发送到终端并执行
        terminal.sendText(textToExecute);
    });

    context.subscriptions.push(executeLineCommand);

    // 添加状态栏项（如果启用）
    const config = vscode.workspace.getConfiguration('sml');
    if (config.get('showStatusBar', true)) {
        const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
        statusBarItem.text = 'SML: 准备就绪';
        statusBarItem.tooltip = '使用 Cmd+Enter (Mac) 或 Ctrl+Enter 执行代码';
        statusBarItem.show();
        context.subscriptions.push(statusBarItem);
    }

    // 注册SML格式化器
    const formatter = new SmlFormatter();
    const formatterProvider = vscode.languages.registerDocumentFormattingEditProvider('sml', formatter);
    const rangeFormatterProvider = vscode.languages.registerDocumentRangeFormattingEditProvider('sml', formatter);

    context.subscriptions.push(formatterProvider);
    context.subscriptions.push(rangeFormatterProvider);
}

export function deactivate() {
    console.log('SML Language Support deactivated');
}